package com.talent.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.talent.page.Page;
import com.talent.pojo.OpenStatus;
import com.talent.service.OpenStatusService;

/**
 * 开放状态
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/openStatus")
public class OpenStatusController  extends BaseController{
	
	@Resource
	private OpenStatusService openStatusService;
	
	/**
	 * 列表查询
	 * @param mv
	 * @param openStatus
	 * @param currentPage
	 * @return
	 */
	@RequestMapping(value = "/manager",method = RequestMethod.GET)
	public ModelAndView manager(OpenStatus openStatus, Integer currentPage,ModelAndView mav){
		try {
			Page<OpenStatus> page = openStatusService.findPage(openStatus, currentPage == null?1:currentPage);
			 mav.addObject("page", page.getResultList());
	            // 分页参数
            initPageParams(mav, page);
            // 搜索条件
            mav.addObject("openStatusList",page.getResultList());
            mav.addObject("openStatus/manager");
		} catch (Exception e) {
			mav = new ModelAndView("error/500");
			e.printStackTrace();
		}
		return mav;
	}
	
	/**
	 * 开放权限
	 * @param id
	 * @return
	 */
	@RequestMapping("/open/{id}")
	@ResponseBody
	public Object openStatus(@PathVariable("id") Integer id){
		int flag = 0;
		try {
			flag = openStatusService.openStatus(id);
			if(flag == 0){
				return renderError("开启该权限失败！");
			}
			return renderSuccess("开启该权限成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return renderError("权限错误。无法开启该权限！");
		}
	}

	/**
	 * 开放权限
	 * @param id
	 * @return
	 */
	@RequestMapping("/close/{id}")
	@ResponseBody
	public Object closeStatus(@PathVariable("id") Integer id){
		int flag = 0;
		try {
			flag = openStatusService.closeStatus(id);
			if(flag == 0){
				return renderError("关闭该权限失败！");
			}
			return renderSuccess("关闭该权限成功！");
		} catch (Exception e) {
			return renderError("权限错误。无法关闭该权限！");
		}
	}
}
